package com.tbabs.controller.front;

import com.tbabs.pojo.*;
import com.tbabs.service.CateService;
import com.tbabs.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class MainController {

    @Autowired
    private CateService cateService;

    @Autowired
    private GoodsService goodsService;

    @RequestMapping("/main")
    public String showAllGoods(Model model, HttpSession session) {

        Integer userid;
        User user = (User) session.getAttribute("user");
        if (user == null) {
            userid = null;
        } else {
            userid = user.getUserid();
        }
        //运动器具
        List<Goods> runGoods = getCateGoods("运动器具", userid);
        model.addAttribute("runGoods", runGoods);
        //日常用品
        List<Goods> dailyGoods = getCateGoods("日常用品", userid);
        model.addAttribute("dailyGoods", dailyGoods);
        //数码产品
        List<Goods> digGoods = getCateGoods("数码产品", userid);
        model.addAttribute("digGoods", digGoods);
        //服装饰品
        List<Goods> dreGoods = getCateGoods("服装饰品", userid);
        model.addAttribute("dreGoods", dreGoods);
        //学习用品
        List<Goods> schGoods = getCateGoods("学习用品", userid);
        model.addAttribute("schGoods", schGoods);
        //书籍
        List<Goods> bookGoods = getCateGoods("书籍", userid);
        model.addAttribute("bookGoods", bookGoods);
        //其他
        List<Goods> othGoods = getCateGoods("其他", userid);
        model.addAttribute("othGoods", othGoods);

        return "front/main";
    }

    public List<Goods> getCateGoods(String cate, Integer userid) {

        //查询分类
        CategoryExample digCategoryExample = new CategoryExample();
        digCategoryExample.or().andCatenameLike(cate);
        List<Category> digCategoryList = cateService.selectByExample(digCategoryExample);
        // 判空
        if (digCategoryList.size() == 0) {
            return null;
        }
        //查询属于刚查到的分类的商品
        GoodsExample digGoodsExample = new GoodsExample();
        List<Integer> digCateId = new ArrayList<>();
        for (Category tmp : digCategoryList) {
            digCateId.add(tmp.getCateid());
        }
        digGoodsExample.or().andCategoryIn(digCateId);
        // 获取对应商品列表
        List<Goods> goodsList = goodsService.selectByExampleLimit(digGoodsExample);
        List<Goods> goodsAndImage = new ArrayList<>();
        //获取每个商品的图片
        for (Goods goods : goodsList) {
            //判断是否为登录状态
            if (userid == null) {
                // 设置无法加入收藏夹
                goods.setFav(false);
            } else {
                // 根据用户id和商品id判断商品是否被加入收藏夹
                Favorite favorite = goodsService.selectFavByKey(new FavoriteKey(userid, goods.getGoodsid()));
                // 根据收藏夹信息设置商品是否被加入收藏夹
                goods.setFav(favorite != null);
            }
            // 获取图片路径
            List<ImagePath> imagePathList = goodsService.findImagePath(goods.getGoodsid());
            goods.setImagePaths(imagePathList);
            // 列表加入图片信息
            goodsAndImage.add(goods);
        }
        // 返回商品和图片列表
        return goodsAndImage;
    }
}
